package io.geekidea.boot.util.baiwangApi;

import com.baiwang.open.client.BWRestClient;
import com.baiwang.open.client.IBWClient;
import com.baiwang.open.client.login.BopLoginClient;
import com.baiwang.open.client.login.BopLoginResponse;
import com.baiwang.open.client.login.PasswordLoginClient;
import com.baiwang.open.client.login.PasswordLoginConfig;
import com.baiwang.open.entity.request.SReceiptQueryReceiptByPageRequest;
import com.baiwang.open.entity.response.SReceiptQueryReceiptByPageResponse;
import com.baiwang.open.exception.BWOpenException;
import io.geekidea.boot.config.baiwang.BaiwangConfig;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 往年销项发票查询
 */
@Component
public class Receipt {
    static String url = BaiwangConfig.getUrl();
    static String appKey = BaiwangConfig.getAppKey();
    static String userName = BaiwangConfig.getUserName();
    static String password = BaiwangConfig.getPassword();
    static String appSecret = BaiwangConfig.getAppSecret();
    static String userSalt = BaiwangConfig.getUserSalt();

    public static void queryReceipt(
            String belongTaxNo,
            String beginDate,
            String endDate,
            String pageNo,
            String pageSize
    ) throws Exception {
        try {
//        String requestId = UUID.randomUUID().toString(); //自定义请求唯一标识
            PasswordLoginConfig loginConfig = new PasswordLoginConfig();
            loginConfig.setUrl(url);
            loginConfig.setClientId(appKey);
            loginConfig.setClientSecret(appSecret);
            loginConfig.setUsername(userName);
            loginConfig.setPassword(password);
            loginConfig.setUserSalt(userSalt);
            BopLoginClient loginClient = new PasswordLoginClient(loginConfig);
            // 获取token, token相关说明请参考 文档中心 -> 用户授权说明
            BopLoginResponse loginResponse = loginClient.login();
            // 重载方法，可以传自定义请求唯一标识
            // BopLoginResponse loginResponse = loginClient.login(requestId);
            String token = loginResponse.getResponse().getAccessToken();
            SReceiptQueryReceiptByPageRequest request = new SReceiptQueryReceiptByPageRequest();
            request.setBeginDate(beginDate);//开始时间
            List<String> invoiceTypeCodeList = new ArrayList<String>();
            invoiceTypeCodeList.add("85");//发票种类编码
            request.setInvoiceTypeCodeList(invoiceTypeCodeList);
            request.setDateType("3");//查询时间类型:1-创建时间,2-状态更新时间,3-开票时间（默认为1）
            request.setDigitInvoiceNo("");//数电发票号码
            request.setEndDate(endDate);//结束时间
            request.setDetailMark("0");//是否需要提供明细 1-是, 0-否(不填默认 0)
            request.setPageNo(pageNo);
            request.setPageSize(pageSize);
            request.setBelongTaxNo(belongTaxNo);//所属税号
            request.setQueryType("2");
            IBWClient client = new BWRestClient(url, appKey, appSecret); // 初始化一个客户端
            //IBWClient client = new BWRestClient(url, appKey, appSecret, loginClient); // 初始化一个客户端,构造函数中传入loginConfig对象参数可针对‘token错误’异常进行重试1次。
            SReceiptQueryReceiptByPageResponse response = client.sReceipt().queryReceiptByPage(request,
                    token);
            // 重载方法，可以传自定义请求唯一标识
            // SReceiptQueryReceiptByPageResponse response = client.sReceipt().queryReceiptByPage(request,token, requestId);
            System.out.println(response);
        } catch (BWOpenException e) {
            throw e;
        }
    }

}
